﻿//创建时间: 2010-6-30
//创建作者: 李城
//功能说明: 信息点击统计逻辑处理类 

using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using TRPOP.CMS.Plugin;
using TRPOP.CMS.UserManage;
using TRPOP.Common;
using TRPOP.CMS.Model;

namespace TRPOP.CMS.Log
{
    public class ContentClickLogManage : IContentClickLog
    {
        ContentClickLogDAL oDal = new ContentClickLogDAL();

        /// <summary>
        ///  添加记录(检测传递参数所对应的内容信息是否有效)
        /// </summary>
        /// <param name="intID"></param>
        /// <returns></returns>
        public bool Save(int intID)
        {
            //是否是有效的内容信息
            if (intID < 0 || oDal.IsvalidityContent(intID) == false)
                return false;
            //初始化信息，赋值，添加到数据库中
            ContentClickLogInfo oInfo = new ContentClickLogInfo();
            oInfo.dtClickTime = DateTime.Now;
            oInfo.iContentID = intID;
            oInfo.iForegroundUserId = 0;
            oInfo.iLogId = 0;
            oInfo.nvcIP = RequestHelper.GetIP();
            oInfo.nvcReferenceURL = RequestHelper.GetUrlReferrer();
            return oDal.Save(oInfo) == EnumSubmitResult.Success;
        }

        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="intID"></param>
        /// <param name="blnIsCheckValidity">是否: 检测传递参数所对应的内容信息是否有效</param>
        /// <returns></returns>
        public bool Save(int intID, bool blnIsCheckValidity)
        {
            //是否是有效的内容信息
            if (blnIsCheckValidity && (intID < 0 || oDal.IsvalidityContent(intID) == false))
                return false;
            //初始化信息，赋值，添加到数据库中
            ContentClickLogInfo oInfo = new ContentClickLogInfo();
            oInfo.dtClickTime = DateTime.Now;
            oInfo.iContentID = intID;
            oInfo.iForegroundUserId = 0;
            oInfo.iLogId = 0;
            oInfo.nvcIP = RequestHelper.GetIP();
            oInfo.nvcReferenceURL = RequestHelper.GetUrlReferrer();
            return oDal.Save(oInfo) == EnumSubmitResult.Success;
        }

        /// <summary>
        ///  根据条件 查询分页数据 （ 必须按照接口定义的函数说明来返回相关的字段 ）
        /// </summary>
        /// <param name="pageNumber">从1开始</param>
        /// <param name="pageSize"></param>
        /// <param name="condition"></param>
        /// <param name="orderby"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public DataSet GetListByPager(int pageNumber, int pageSize, string condition, string orderby, out int recordCount)
        {
            recordCount = oDal.GetDataRowCount(GetBaseSQL(condition, orderby));
            if (recordCount <= 0)
                return null;
            if (pageSize > 0 && pageNumber > 0 && ((pageNumber - 1) * pageSize + 1) > recordCount)
                return null;
            return oDal.GetDataSet(GetBaseSQL_Pager(orderby, condition, pageNumber, pageSize));
        }

        /// <summary>
        /// 统计点击量的基本SQL
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        private static string GetBaseSQL(string condition, string strOrderby)
        {
            //排序规则
            string strOrderbyForSQL;
            if (strOrderby.IsEmpty())
            {
                strOrderbyForSQL = " ORDER BY  BaseInfo.ContentId DESC ";
            }
            else
            {
                strOrderbyForSQL = " ORDER BY   " + strOrderby + "  ";
            }
            return string.Format(@"
                                    SELECT 
                                        ROW_NUMBER() OVER ( {2} )  AS DataIndexNum,
	                                    TableGroup.ClickCount,
	                                    BaseInfo.*
                                    FROM
	                                    (
		                                    SELECT 
			                                      COUNT(contentid) AS ClickCount,
			                                      contentid
		                                    FROM {1}_vwContentClickLog
		                                    WHERE 1=1 {0}
		                                    GROUP BY contentid
	                                    ) TableGroup
	                                    LEFT OUTER JOIN 
                                        {1}_vwContentBaseInfoForClick BaseInfo ON BaseInfo.contentid=TableGroup.contentid
                                  "
                                , condition, SystemInfoHelper.getSystemInfo().DBprefixTableName, strOrderbyForSQL
                                );
        }

        /// <summary>
        /// 基本的SQL 这样查找出了所有的按照
        /// </summary>
        /// <param name="strOrder">条件约束</param>
        /// <param name="strCondition">排列顺序</param>
        /// <returns></returns>
        private static string GetBaseSQL_Pager(string strOrder, string strCondition, int intPageNumber, int intPageSize)
        {
            //分页规则
            string strWhere = string.Empty;
            if (intPageNumber > 0 && intPageSize > 0)
            {
                strWhere += " AND  DataIndexNum <= " + (intPageNumber * intPageSize) + "  ";
                if (intPageNumber > 1)
                {
                    strWhere += " AND DataIndexNum>=  " + ((intPageNumber - 1) * intPageSize + 1) + "  ";
                }
            }
            return string.Format(@" 
                                    WITH DataView AS
                                    (
                                        {0}
                                    )
                                    SELECT  DataView.*
                                    FROM DataView
                                    WHERE 1=1  {1} 
                                  "
                                , GetBaseSQL(strCondition, strOrder), strWhere
                                );
        }

        /*  测试成功的原型 SQL
         * 
         * 
                WITH DataView AS
                (
	                SELECT 
		                ROW_NUMBER() OVER ( ORDER BY BaseInfo.contentid desc )  AS DataIndexNum,
		                TableGroup.ClickCount,
		                BaseInfo.*
	                FROM
		                (
			                SELECT 
				                  COUNT(contentid) AS ClickCount,
				                  contentid
			                FROM cms_vwContentClickLog
			                WHERE 1=1
			                GROUP BY contentid
		                ) TableGroup
		                LEFT OUTER JOIN 
		                cms_vwContentBaseInfoForClick BaseInfo ON BaseInfo.contentid=TableGroup.contentid
                )
                SELECT  DataView.*
                FROM DataView
                WHERE DataIndexNum>=1 and DataIndexNum<=2
         * 
         * 
         */
    }
}
